import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.sound.sampled.Line;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicComboBoxUI.KeyHandler;

/**
 * 
 */

/**
 * @author trunghpb
 *
 */
public class PaintPanel extends JPanel {
	
	private Point2D	 last;
	private ArrayList<Line2D> lines;
	
	public static final int SMALL_INCREMENT = 1;
	public static final int LARGE_INCREMENT = 5;
	
	public PaintPanel() {
		last = new Point2D.Double(100, 100);
		lines = new ArrayList<Line2D>();
		
		KeyHandler listener = new KeyHandler();
		this.addKeyListener(listener);
		this.setFocusable(true);
	}
	
	public void add(int dx, int dy){
		Point2D end = new Point2D.Double(last.getX() + dx, last.getY() + dy);
		
		Line2D line = new Line2D.Double(last, end);
		lines.add(line);
		repaint();
		
		last = end;
	}
	
	@Override
	public void paintComponent(Graphics g) {	
		super.paintComponent(g);
		
		Graphics2D g2 = (Graphics2D) g;
		for(Line2D l:lines)g2.draw(l);
	}
	
	class KeyHandler implements KeyListener{
		
		public KeyHandler() {}
		
		public void keyPressed(KeyEvent event) {
			int KeyCode = event.getKeyCode();
			
			int d;
			if (event.isShiftDown()){
				d = LARGE_INCREMENT;
			}else {
				d = SMALL_INCREMENT;
			}
			
			switch (KeyCode){
			case KeyEvent.VK_LEFT:
				add(-d,0);
				break;
			case KeyEvent.VK_RIGHT:
				add(d, 0);
				break;
			case KeyEvent.VK_UP:
				add(0,-d);
				break;
			case KeyEvent.VK_DOWN:
				add(0,d);
				break;
			}
		}
		
		public void keyReleased(KeyEvent arg0) {}
		public void keyTyped(KeyEvent arg0) {}	
		
	}
}
